﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TSharp.Core.Authorization.Resources
{
    #region 注释
    /* ====================
     * 作者：wangyao
     * 创建时间：2013/5/22 10:41:17
     * ====================
     */
    #endregion
    /// <summary>
    /// 资源操作类型类
    /// </summary>
    public sealed class Operation
    {
        /// <summary>
        /// 操作ID
        /// </summary>
        public readonly int id;

        /// <summary>
        /// 操作名称
        /// </summary>
        public readonly string title;

        /// <summary>
        /// 可覆盖操作ID
        /// </summary>
        private int[] coverOperations;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="id">操作ID</param>
        /// <param name="title">操作名称</param>
        /// <param name="coverOperations"></param>
        public Operation(int id, string title, int[] coverOperations)
        {
            this.id = id;
            this.title = title;
            this.coverOperations = coverOperations;
        }

        /// <summary>
        /// 获取可覆盖操作数组长度
        /// </summary>
        public int CoverOperationSize
        {
            get
            {
                return coverOperations != null ? coverOperations.Length : 0;
            }
        }

        /// <summary>
        /// 根据索引值获取操作ID
        /// </summary>
        /// <param name="index">索引值</param>
        /// <returns>操作ID</returns>
        public int GetCoverOperation(int index)
        {
            return coverOperations != null ? coverOperations[index] : 0;
        }

        /// <summary>
        /// 处理函数待处理函数方法实现了当拥有一项权限的时候，是否还拥有指定权限的判断
        /// </summary>
        /// <param name="operationId"></param>
        /// <returns></returns>
        public bool IsSuperOf(int operationId)
        {
            if (coverOperations != null)
            {
                for (int i = 0; i < coverOperations.Length; i++)
                {
                    if (coverOperations[i] == operationId)
                    {
                        return true;
                    }
                }
            }
            return false;
        }
    }
}
